package com.wwwarehouse.resource_center.fragment.information;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.PaginationItemOrResourceBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.util.HashMap;
import java.util.Map;

import static com.wwwarehouse.resource_center.constant.ResourceConstant.PATH_CREATEWAREHOUSEFRAGMENT;

/**
 * @author Joseph on 2017/9/25.
 *         <p/>
 *         创建物
 *         <p>
 *         物的分类：H_GOODS(商品)、H_TOOLS(工具)、H_SHOP(店铺)、H_STOCK(仓库)、
 *         资源的分类：H_SALE(供货)、H_STORE(仓储)、H_LABOUR(劳务)、H_PROCESS(加工)、H_SELL(销售)、H_TRANSPORT(运输)
 */
@Route(path = PATH_CREATEWAREHOUSEFRAGMENT)
public class GoodsInfomationCreateItemFragment extends MyBaseViewPagerFragment {

    int mNum = 7;
    public static final int PAGINATION_ITEMORRESOURCE = 1;  //
    private String businessId = "";          //业务单位ID
    private String type = "";


    @Override
    public void requestDatas() {
        Bundle bundle = getArguments();
        if (null!=bundle){
            businessId = bundle.getString("businessId");
            type = bundle.getString("type");
        }
        mActivity.setTitle(type.equals("1")?getString(R.string.infomation_create_item):getString(R.string.infomation_create_resources));
        sp.putValue("businessId",businessId);
        pagination();
    }

    private void pagination() {
        Map map = new HashMap();
        Map<String, Object> query = new HashMap<>();
//        map.put("businessId", businessId);
        map.put("itemType", "");//按分类查询物或资源，非必须
        map.put("name", "");                 //按名称模糊查询，非必须
        map.put("type", type);   // 物：1、资源：0，必须
        query.put("page", 1);
        query.put("size", 7);
        query.put("sort", "");
        map.put("query", query);
        httpPost(ResourceConstant.PAGINATION_ITEMORRESOURCE, map, PAGINATION_ITEMORRESOURCE, false, "");
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case PAGINATION_ITEMORRESOURCE:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    PaginationItemOrResourceBean goodsSelectLabelBean = JSON.parseObject(commonClass.getData().toString(), PaginationItemOrResourceBean.class);
                    int total = (int) goodsSelectLabelBean.getTotal();
                    if (total<=0){
                        mLoadingView.setVisibility(View.VISIBLE);
                        mLoadingView.showEmptyView(true);
                        mLoadingView.setEmptyListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mLoadingView.showProgressView(true);
                                pagination();
                            }
                        });
                        return;
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("type", type);   //type
                    SetFragmentAndPage(total, mNum,new GoodsInfomationListFragment().getClass().getName(),bundle);
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {
        pagination();
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsInfomationCreateItemFragment){
            if (!TextUtils.isEmpty(type)){
                LogUtils.showInfoLog("GoodsInfomationCreateItemFragment");
                mActivity.setTitle(type.equals("1")?getString(R.string.infomation_create_item):getString(R.string.infomation_create_resources));
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
